package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.PaymentMethod;
import com.ericom.service.GenericManager;

public class PaymentMethodAction extends BaseAction {
	private GenericManager<PaymentMethod, Long> paymentMethodManager;
	private List paymentMethods;
	private PaymentMethod paymentMethod;
	private Long id;

	public PaymentMethod getPaymentMethod() {
		return paymentMethod;
	}

	public void setPaymentMethod(PaymentMethod paymentMethod) {
		this.paymentMethod = paymentMethod;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setPaymentMethodManager(
			GenericManager<PaymentMethod, Long> paymentMethodManager) {
		this.paymentMethodManager = paymentMethodManager;
	}

	public String list() {
		paymentMethods = paymentMethodManager.getAll();
		return SUCCESS;
	}

	public List getPaymentMethods() {
		return paymentMethods;
	}

	public String delete() {
		paymentMethodManager.remove(paymentMethod.getId());
		this.saveMessage(getText("paymentMethod.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			paymentMethod = paymentMethodManager.get(id);
		} else {
			paymentMethod = new PaymentMethod();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (paymentMethod.getId() == null);
		paymentMethod = paymentMethodManager.save(paymentMethod);

		String key = (isNew) ? "paymentMethod.added" : "paymentMethod.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
